package com.lujianfei.powerinput.ui.template.presenter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.lujianfei.powerinput.database.TemplateFieldModel;
import com.lujianfei.powerinput.database.TemplateModel;
import com.lujianfei.powerinput.ui.template.TemplateAddTypeActivity;
import com.lujianfei.powerinput.ui.template.base.TemplateAddContract;
import com.lujianfei.powerinput.utils.ConfigUtils;
import com.lujianfei.powerinput.utils.ToastUtils;
import com.lujianfei.powerinput.widget.input.factory.InputFactory;
import com.lujianfei.powerinput.widget.input.base.IWidgetInput;

import java.util.ArrayList;
import java.util.List;

public class TemplateAddPresenter implements TemplateAddContract.Presenter {
    private static final String TAG = "TemplateAddPresenter";
    private static final int REQUEST_ADD_TYPE = 100;
    private final TemplateAddContract.View view;
    private IWidgetInput textInput;
    private TemplateModel data;
    public TemplateAddPresenter(TemplateAddContract.View view) {
        this.view = view;
    }

    @Override
    public void init() {
        textInput = InputFactory.newInput("com.lujianfei.powerinput.widget.input.TextInput",view.currentActivity());
        if(null!=textInput) {
            textInput.setWidgetLabel("名称:");
            textInput.setWidgetKey("templateName");
            view.getContainer().addView(textInput.getWidgetView(), view.getContainer().getChildCount() - 1);
            if (null != data) {
                textInput.setWidgetValue(data.getName());
                try {
                    List<TemplateFieldModel> templateFieldModels = TemplateFieldModel.findWithQuery(TemplateFieldModel.class, "select * from TemplateFieldModel where templateId=?", "" + data.getId());
                    if (null != templateFieldModels) {
                        for (TemplateFieldModel bean :
                                templateFieldModels) {
                            final IWidgetInput textInputNew = InputFactory.newInput(bean.getClassName(), view.currentActivity());
                            if (null!=textInputNew) {
                                textInputNew.showDelete();
                                textInputNew.setWidgetClassName(bean.getClassName());
                                textInputNew.setWidgetLabel(textInputNew.getWidgetName());
                                textInputNew.setWidgetValue(bean.getKey());
                                textInputNew.setOnWidgetDeleteListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view2) {
                                        view.getContainer().removeView(textInputNew.getWidgetView());
                                    }
                                });
                                view.getContainer().addView(textInputNew.getWidgetView(), view.getContainer().getChildCount() - 1);
                            }
                        }
                    }
                } catch (Exception e) {
                    Log.e(TAG, "" + e);
                }
            }
        }

    }

    @Override
    public void addRow() {
        view.currentActivity().startActivityForResult(new Intent(view.currentActivity(),TemplateAddTypeActivity.class),REQUEST_ADD_TYPE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode!=Activity.RESULT_OK){
            return;
        }
        switch (requestCode){
            case REQUEST_ADD_TYPE:{
                ConfigUtils.ConfigEntity configEntity = (ConfigUtils.ConfigEntity) data.getSerializableExtra("data");
                final IWidgetInput textInput = InputFactory.newInput("com.lujianfei.powerinput.widget.input.TextInput",view.currentActivity());
                if(null!=textInput) {
                    textInput.showDelete();
                    textInput.setWidgetLabel(textInput.getWidgetName());
                    textInput.setWidgetClassName(configEntity.getClassName());
                    textInput.setOnWidgetDeleteListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view2) {
                            view.getContainer().removeView(textInput.getWidgetView());
                        }
                    });
                    view.getContainer().addView(textInput.getWidgetView(), view.getContainer().getChildCount() - 1);
                }
            }
            break;
            default:
                break;
        }
    }

    @Override
    public void onTitleRightImageClick() {
        String widgetValue = textInput.getWidgetValue();
        if (TextUtils.isEmpty(widgetValue)){
            ToastUtils.show("名称不能为空");
            return;
        }
        if (null == data){
            //新增
            TemplateModel templateModel = new TemplateModel();
            templateModel.setName(widgetValue);
            long saveId = templateModel.save();
            viewToDb(saveId);
        }else{
            try {
                //编辑
                List<TemplateFieldModel> templateFieldModels = TemplateFieldModel.find(TemplateFieldModel.class, "templateId=?", "" + data.getId());
                //清空原有
                for (TemplateFieldModel templateFieldModel1 :
                        templateFieldModels) {
                    templateFieldModel1.delete();
                }
            }catch (Exception e){
                Log.e(TAG,""+e);
            }
            viewToDb(data.getId());
        }
        view.currentActivity().finish();
    }

    private void viewToDb(long templateId) {
        List<IWidgetInput> childs = getChilds();
        for (IWidgetInput widgetInput:
        childs) {
            TemplateFieldModel templateFieldModelNew = new TemplateFieldModel();
            templateFieldModelNew.setTemplateId(templateId);
            templateFieldModelNew.setClassName(widgetInput.getWidgetClassName());
            templateFieldModelNew.setKey(widgetInput.getWidgetValue());
            templateFieldModelNew.save();
        }
    }

    @Override
    public void setData(TemplateModel data) {
        this.data = data;
    }

    private List<IWidgetInput> getChilds(){
        int childCount = view.getContainer().getChildCount();
        List<IWidgetInput> widgetInputs = new ArrayList<>();
        for (int i = 0; i < childCount; i++) {
            View childAt = view.getContainer().getChildAt(i);
            if (childAt instanceof IWidgetInput){
                IWidgetInput childAtNew = (IWidgetInput) childAt;
                if(i>0) {
                    widgetInputs.add(childAtNew);
                }
            }
        }
        return widgetInputs;
    }
}
